package com.zhongli.devplatform.utils.user;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Encrypt {
public static String getMD5Password(String password) throws NoSuchAlgorithmException {
		
		String salt = "zhongli";
		password += salt;
		MessageDigest md = MessageDigest.getInstance("MD5");
		
		md.update(password.getBytes());
		 
		StringBuffer buffer = new StringBuffer();
		byte[] result = md.digest();
        for (byte b : result) {
            // 与运算
            int number = b & 0xff;// 加盐
            String str = Integer.toHexString(number);
            if (str.length() == 1) {
                buffer.append("0");
            }
            buffer.append(str);
        }

        // 标准的md5加密后的结果
        return buffer.toString();
	}
	
	public static String base64encode(String str) {
		Base64.Encoder encoder = Base64.getEncoder();
		byte[] textByte;
		try {
			textByte = str.getBytes("UTF-8");
			String encodedText = encoder.encodeToString(textByte);
			return encodedText;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String base64decode(String str) {
		Base64.Decoder decoder = Base64.getDecoder();
		try {
			String string  = new String(decoder.decode(str), "UTF-8");
			return string;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
